package com.zit.java.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.zit.java.pojo.EntrustList;
import com.zit.java.pojo.EntrustStatus;
import com.zit.java.service.entrust.EntrustService;
import com.zit.java.util.object.TableData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("entrust")
public class EntrustController extends HttpServlet {

    @Autowired
    private EntrustService entrustService;

    @RequestMapping("toEntrustPag")
    public String toEntrustPag(Model model) {
        List<EntrustList> list = entrustService.queryAllDate();
        model.addAttribute("list", list);
        return "entrust/entrustlist";
    }

    /**
     * 显示清单列表
     *
     * @return
     */
    @RequestMapping("showlist")
    @ResponseBody
    public TableData<EntrustList> showTable() {
        TableData<EntrustList> tableData = new TableData();
        List<EntrustList> list = entrustService.queryAllDate();
        tableData.setMsg("成功");
        tableData.setCode(0);
        tableData.setData(list);
        tableData.setCount(list.size());
        return tableData;
    }


    /**
     * 通过模糊名字查询
     *
     * @param listname
     * @return
     */

    @RequestMapping("queryByName")
    @ResponseBody
    public TableData<EntrustList> queryTableByName(@RequestParam("listname") String listname) {
        TableData<EntrustList> tableData = new TableData();
        List<EntrustList> entrustLists = entrustService.queryDataByName(listname);
        tableData.setCode(0);
        tableData.setMsg("ok");
        tableData.setCount(entrustLists.size());
        tableData.setData(entrustLists);
        return tableData;
    }

    /**
     * 导出excel功能
     *
     * @param response
     */
    @RequestMapping("exportexcel")
    public void exportExcel(HttpServletResponse response) {
        //设置excel类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //设置文件文件名字
        response.setHeader("Content-Disposition", "attachment;filename=" + DateUtil.now() + ".xls");
        //查询所有数据将数据映射为map集合
        List<Map<String, String>> mapList = entrustService.selectAllData();
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter();
        //写入表格
        writer.write(mapList, true);
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            writer.flush(out, true);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
        IoUtil.close(out);


    }

    /**
     * 到模板查询页面
     *
     * @return
     */
    @RequestMapping("totemplatequery")
    public String templateQuery() {
        return "entrust/templatequery";
    }

    /**
     * 下载委托模板
     *
     * @param response
     */
    @RequestMapping("downloadtable")
    public void downloadTable(HttpServletResponse response) {
        //设置文件
        response.setContentType("application/x-download");
        //设置文件文件名字
        response.setHeader("Content-Disposition", "attachment;filename=" + DateUtil.now() + ".doc");
        //获取本地文docx的绝对路径
        String fileAbsolutPath = "D:\\complete-roject\\mvc\\src\\main\\java\\com\\zit\\java\\tempalte\\salaryentrusttext.doc";
        //获取写入流
        ServletOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        try {
            outputStream = response.getOutputStream();
            fileInputStream = new FileInputStream(fileAbsolutPath);
            byte[] b = new byte[1024 * 10];
            int i = 0;
            while ((i = fileInputStream.read(b)) > 0) {
                outputStream.write(b, 0, i);
            }
            outputStream.flush();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 到委托情况查询页面
     *
     * @return
     */
    @RequestMapping("entrustShow")
    public String entrustStatusShow() {
        return "entrust/entruststatus";

    }


    /***
     * 返回委托情况的数据
     */

    @RequestMapping("/entrusData")
    @ResponseBody
    public TableData<EntrustStatus> restEntrustData() {
        TableData<EntrustStatus> rstTableData = new TableData<>();
        List<EntrustStatus> list = entrustService.queryAllErustStatus();
        rstTableData.setData(list);
        rstTableData.setMsg("ok");
        rstTableData.setCode(0);
        return rstTableData;
    }

    /**
     *
     * @return
     */
    @RequestMapping("/showlistByName")
    @ResponseBody
    public TableData showOutSchoolQuery(@Param("name") String name) throws ClassNotFoundException, SQLException {
        TableData<Object> data = new TableData<>();
        List<EntrustList> list = entrustService.queryDataByName(name);
        data.setData(list);
        data.setCode(0);
        data.setMsg("ok");

        return data;
    }

}
